#!/bin/bash

cd /home/qie_global/code/run

SET_ID=1

echo "stop verifysvrd namesvrd globalsvrd ?[yes/no] to continu"
read -s -r -p"==>[yes/no]:"  IF_CONTINU;

if [ "yes" = $IF_CONTINU ]
then
   echo "begin to stop verifysvrd namesvrd globalsvrd "
else
    echo "you choose no to exit"
    
    exit 0
fi

red="\033[31m"
green="\033[32m"
yellow="\033[33m"
no="\033[0m"

svr_list=`echo /home/qie_global/code/run/{verifysvrd,namesvrd,globalsvrd}`
cd /home/qie_global/code/run

rm -rf ./cid/3.cid
rm -rf ./cid/3.cid

rm -rf ./cid/10.cid
rm -rf ./cid/11.cid

rm -rf ./tid/10.tid
rm -rf ./tid/11.tid


if [ ! -s /home/qie_global/code/run/mon_global_qie.pid ]
then
   if pgrep -U df -f mon_global_qie.sh &> /dev/null
   then
        kill -9 `pgrep -f  mon_global_qie.sh`
      echo echo -e "$green $(date) kill mon process ok $no"
   else
      echo -e "$yellow $(date) the mon process not running $no"
   fi
else
     kill -9 `cat /home/qie_global/code/run/mon_global_qie.pid` &> /dev/null  
   sleep 1
   if pgrep -U df  -f mon_global_qie.sh &> /dev/null
   then
      echo -e "$red $(date) the mon process stop err $no"
   else
      echo -e "$green $(date) kill mon process ok $no"
      rm -f /home/qie_global/code/run/mon_global_qie.pid
   fi
fi 

stop()
{
list=$1
for i in $svr_list
do
   cd $i
   j=`echo ${i##*/}` 
   m=`tr -d '[0-9]' <<< $j`
   if [ -s ${m}.pid ]
   then 
      read SVC_PID < ${m}.pid
   else
      read SVC_PID <<< 0
   fi
   if [ -s commsvrd.pid ]
   then
      read com < commsvrd.pid
   else
      read com <<< 0  
   fi

   if [ $SVC_PID = 0 ]
   then
      if pgrep -U df -f  "$m.*$j" &> /dev/null
      then
           kill -9 `pgrep -U df -f  "$m.*$j"`
         echo -e "$green $(date) kill  $j $m ok  $no"
      else
         echo -e "$yellow $(date) $j $m not running $no"
      fi
   else
        kill -3 $SVC_PID &> /dev/null
      while true
      do
        kill -0 $SVC_PID  &> /dev/null ||  break 
      sleep 1
      done
      sleep 1
      if pgrep -U df -f  "$m.*$j" &> /dev/null
      then
           kill -9 `pgrep -U df -f  "$m.*$j"`
         echo -e "$green $(date) kill  $j  $m ok $no"
         rm -f ${m}.pid
      else
         echo -e "$green $(date) SIGQUIT $j  $m ok  $no"
         rm -f ${m}.pid
      fi
   fi

   if [ $com = 0 ]
   then  
      if pgrep -U df -f  "commsvrd.*$j" &> /dev/null
      then
           kill -9 `pgrep -U df -f  "commsvrd.*$j"`
         echo -e "$green $(date) kill  $j commsvrd ok  $no" 
      else
         echo -e "$yellow $(date) $j commsvrd not running $no"
      fi
   else
        kill -3 $com &> /dev/null
      sleep 1
      if pgrep -U df  -f  "commsvrd.*$j" &> /dev/null
      then
           kill -9 `pgrep -U df  -f  "commsvrd.*$j"`
         echo -e "$green $(date) kill  $j  commsvrd ok  $no"
         rm -f commsvrd.pid
      else
         echo -e "$green $(date) SIGQUIT $j commsvrd $no"
         rm -f commsvrd.pid
     fi
   fi
done
}

stop "$svr_list"

mmapdir=`echo /home/mmap/qie_global`
umount $mmapdir



